package Demo;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo02 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> q = new LinkedBlockingQueue<>();

        Thread customer = new Thread( () -> {
            while (true) {
                try {
                    int elem = q.take();
                    System.out.println("消费元素: " + elem);
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread producer = new Thread( () -> {
           Random random = new Random();
           while (true) {
               try {
                   int num = random.nextInt(100);
                   System.out.println("生产元素: " + num);
                   q.put(num);
                   Thread.sleep(10);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });

        customer.start();
        producer.start();

        customer.join();
        producer.join();
    }
}
